package com.xmy.cultivate.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.InterflowCommunity;
import com.xmy.cultivate.entity.OrderDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 沟通社区 Mapper 接口
 * </p>
 *
 * @author Helin
 * @since 2024-03-02
 */
@Mapper
public interface InterflowCommunityMapper extends BaseMapper<InterflowCommunity> {

    /**
     * 员工信息列表
     *
     * @param type
     * @param staffName
     * @return
     */
    @Select({"<script>",
            "WITH RankedCommunities AS ( ",
            "SELECT ",
            "staff_id,",
            "compile_num,",
            "interflow_community.created_at,",
            "getSchoolName ( staff.school_id ) AS schoolName, ",
            "staff.`name` AS staff_name,",
            "dense_rank() over ( ORDER BY compile_num DESC ) AS 'rank',",
            "ROW_NUMBER() OVER (PARTITION BY staff_id ORDER BY created_at DESC) AS rn ",
            "FROM ",
            "interflow_community ",
            "LEFT JOIN staff ON interflow_community.staff_id=staff.id ",
            "WHERE ",
            "interflow_community.deleted = 0 ",
            "<if test='staffName != null and staffName != \"\"'>",
            "AND staff.`name` like concat('%', #{staffName}, '%') ",
            "</if>",
            ") ",
            "SELECT ",
            "* ",
            "FROM ",
            "RankedCommunities ",
            "WHERE ",
            "rn = 1 ",
            "ORDER BY ",
            "<when test='type == 1'>",
            "created_at DESC ",
            "</when>",
            "<when test='type == 2'>",
            "compile_num DESC",
            "</when>",
            "</script>"})
    public abstract IPage<InterflowCommunity> staffList(IPage<?> page,@Param("type") String type, @Param("staffName") String staffName);

    /**
     * 员工信息内容
     *
     * @param id
     * @param staffId
     * @return
     */
    @Select({"<script>",
            "SELECT  ",
            "staff_id, ",
            "text,",
            "compile_num,",
            "text,",
            "interflow_community.created_at, ",
            "getStaffNameForId(staff_id) AS staffName ",
            "FROM ",
            "interflow_community ",
            "WHERE ",
            "interflow_community.deleted = 0 ",
            "AND staff_id = #{staffId} ",
            "<if test='id != null and id != \"\"'>",
            "AND interflow_community.id = #{id} ",
            "</if>",
            "ORDER BY created_at DESC",
            "LIMIT 1",
            "</script>"})
    public abstract InterflowCommunity staffInformation(@Param("id") String id, @Param("staffId") String staffId);


}

